<?

require_once("system/config.php");
session_start();
session_set_cookie_params(1000);

require_once("xajax/xajax.inc.php");
$smarty->assign("debug", $debug);

$xajax = new xajax();
//$xajax->setCharEncoding('windows-1251');
//$xajax->decodeUTF8InputOn();

$xajax->registerFunction("orderItem");
$xajax->registerFunction("updateShoppingCart");
$xajax->registerFunction("changeItemStatus");
$xajax->registerFunction("emptySC");

$xajax->registerFunction("showPage");
$xajax->registerFunction("getArticle");
$xajax->registerFunction("getTopic");


function showPage($pageName){
    global $smarty, $decor;
	if(empty($pageName)){$pageName = "errors/errorBadRequest";}
	if(!$smarty->template_exists($pageName.".tpl")){$pageName = "errors/errorNoTemplate";}
	$objResponse = new xajaxResponse();
    $newContent = $smarty->fetch($pageName.".tpl");
	$newContent = $decor->addCanvas($newContent);
	$objResponse->addAssign("Container","innerHTML", $newContent);
    return $objResponse;
}

function getTopic($topicName){
   global $smarty, $decor, $logger;
   $pages = new pages();
   $objResponse = new xajaxResponse();
   try {
      $articles = $pages->getTopic($topicName);
	  $path = $pages->getPath();
      $smarty->assign("path", $path);
	  $smarty->assign("articles", $articles);
	  $newContent = $smarty->fetch($articles[0]['_pages_page_template'].".tpl");
   } catch (Exception $e) {
      $newContent = $e->getMessage();
   }
   
   $newContent = iconv("windows-1251", "UTF-8", $decor->addCanvas($newContent));;
   $objResponse->addAssign("Container","innerHTML", $newContent);
   //$logger->log($objResponse->{xml});
   
 return $objResponse;
}


function getArticle($artId){
   global $smarty, $decor, $logger;
   $pages = new pages();
   $objResponse = new xajaxResponse();
   try {
      $article = $pages->getArticle($artId);
	  $path = $pages->getPath();
	  $smarty->assign("path", $path);
	  $smarty->assign("articles", $article);
	  $newContent = $smarty->fetch($article[0]['_pages_page_template']."_details.tpl");
   } catch (Exception $e) {
      $newContent = $e->getMessage();
   }
   
   $newContent = iconv("windows-1251", "UTF-8", $decor->addCanvas($newContent));
   $objResponse->addAssign("Container","innerHTML", $newContent);
 return $objResponse;
}


function showSlide()
{
    global $smarty, $decor;
    $objResponse = new xajaxResponse();
	$images = getImagesList();
	$smarty->assign("images", $images);
	$imagesJSList = $smarty->fetch("imagesList.tpl");
	$newContent = $decor->addCanvas($newContent);
	//$objResponse->addAssign("slideJsContainer","innerHTML", $imagesJSList);
	return $imagesJSList;
}

function orderItem($item)
{
    global $smarty, $decor;
	$shop = new shop(session_id());
	$shop->orderItem($item);
    
	$items = $shop->getBookedItems();
   
   $objResponse = new xajaxResponse();
   //print_r($items);
   $smarty->assign("items", $items);
   $newContent = $smarty->fetch("shoppingCart.tpl");
   
   $objResponse->addAssign("shoppingCart","innerHTML", $newContent);
	
   return $objResponse;
}

function updateShoppingCart()
{
   global $smarty, $decor;
   $shop = new shop(session_id());   
   $items = $shop->getBookedItems();
   
   $objResponse = new xajaxResponse();
   //print_r($items);
   $smarty->assign("items", $items);
   $newContent = $smarty->fetch("shoppingCart.tpl");
   
   $objResponse->addAssign("shoppingCart","innerHTML", $newContent);
	
   return $objResponse;
}

function changeItemStatus($itemObj)
{
   global $smarty, $decor;
   $shop = new shop();   
   $shop->removeItem();
   
   updateShoppingCart();
   /*
   $objResponse = new xajaxResponse();
   $smarty->assign("message", "You've ordered $item");
   $newContent = $smarty->fetch("shoppingCart.tpl");
   // $objResponse->addAssign("imgContainer","innerHTML", $newContent);
   $objResponse->addAssign("shoppingCart","innerHTML", $newContent);
	
    return $objResponse;*/
}

function emptySC() {
     global $smarty, $decor;
   $shop = new shop();   
   $shop->emptyShoppingCart();
   
   $items = $shop->getBookedItems();
   $objResponse = new xajaxResponse();
   $smarty->assign("items", $items);
   $newContent = $smarty->fetch("shoppingCart.tpl");
   $objResponse->addAssign("shoppingCart","innerHTML", $newContent);
   return $objResponse;
}

function getImagesList() {
  if ($handle = opendir(WALLS_DIR)) {
   $i = 0;
   while (false !== ($file = readdir($handle))) 
   {
      if ( preg_match( "/[.]jpg$/", $file ) ) {
         list($width, $height, $type, $attr) = getimagesize(WALLS_DIR."/".$file);
         $images[$i]['src'] = $file;
		 $images[$i]['width'] = $width;
		 $images[$i]['height'] = $height;
		 $i++;
	  }
   }
    closedir($handle);
  }
  shuffle($images);
  return $images;
}


$xajax->processRequests();
$smarty->assign("jsList", showSlide());
$smarty->assign("xAjaxJS", $xajax->getJavascript("xajax/"));
$smarty->assign("site_title", $site_title);
$smarty->assign("meta_keywords", $meta_keywords);

session_write_close();
$smarty->display("mainWindow.tpl");
?>